package com.ruoyi.web.controller.guarantee;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.apache.catalina.User;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.guarantee.domain.BBidding;
import com.ruoyi.guarantee.service.IBBiddingService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 投标保函Controller
 * 
 * @author ruoyi
 * @date 2024-11-05
 */
@RestController
@RequestMapping("/guarantee/bidding")
public class BBiddingController extends BaseController
{
    @Autowired
    private IBBiddingService bBiddingService;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询投标保函列表
     */
    @PreAuthorize("@ss.hasPermi('guarantee:bidding:list')")
    @GetMapping("/list")
    public TableDataInfo list(BBidding bBidding)
    {
        bBidding.setUserId(getUserId());
        startPage();
        List<BBidding> list = bBiddingService.selectBBiddingList(bBidding);
        return getDataTable(list);
    }

    /**
     * 导出投标保函列表
     */
    @PreAuthorize("@ss.hasPermi('guarantee:bidding:export')")
    @Log(title = "投标保函", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BBidding bBidding)
    {
        List<BBidding> list = bBiddingService.selectBBiddingList(bBidding);
        ExcelUtil<BBidding> util = new ExcelUtil<BBidding>(BBidding.class);
        util.exportExcel(response, list, "投标保函数据");
    }

    /**
     * 获取投标保函详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bBiddingService.selectBBiddingById(id));
    }

    /**
     * 新增投标保函
     */
    @PreAuthorize("@ss.hasPermi('guarantee:bidding:add')")
    @Log(title = "投标保函", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BBidding bBidding)
    {
        bBidding.setStatus(0L);
        bBidding.setUserId(getUserId());
        bBidding.setCreateBy(getUsername());
        bBidding.setCreateTime(new Date());
        return toAjax(bBiddingService.insertBBidding(bBidding));
    }

    /**
     * 修改投标保函
     */
    @PreAuthorize("@ss.hasPermi('guarantee:bidding:edit')")
    @Log(title = "投标保函", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BBidding bBidding)
    {
        return toAjax(bBiddingService.updateBBidding(bBidding));
    }


    /**
     * 提交投标保函
     */
    @PreAuthorize("@ss.hasPermi('guarantee:bidding:edit')")
    @Log(title = "投标保函", businessType = BusinessType.INSERT)
    @PostMapping("/tijiao/{id}")
    public AjaxResult tijiao(@PathVariable Long id)
    {
        BBidding bBidding = bBiddingService.selectBBiddingById(id);
        SysUser sysUser = userService.selectUserById(getUserId());
         List<Long> roleIds =sysUser.getRoles().stream().map(SysRole::getRoleId).collect(Collectors.toList());
        if (roleIds.contains(100L)){//业务主管
            bBidding.setStatus(4L);
        }else {
            bBidding.setStatus(2L);
        }
        bBidding.setUpdateBy(getUsername());
        bBidding.setUpdateTime(new Date());
        return toAjax(bBiddingService.updateBBiddingStatus(bBidding));
    }

    /**
     * 删除投标保函
     */
    @PreAuthorize("@ss.hasPermi('guarantee:bidding:remove')")
    @Log(title = "投标保函", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bBiddingService.deleteBBiddingByIds(ids));
    }

    /**
     * 查询历史投标保函列表
     */
    @PreAuthorize("@ss.hasPermi('guarantee:bidding:list')")
    @GetMapping("/historyList")
    public TableDataInfo historyList(BBidding bBidding)
    {
        startPage();
        List<BBidding> list = bBiddingService.selectHistoryBiddingList(bBidding);
        return getDataTable(list);
    }
}
